﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using Microsoft.Win32;
using HA.ModFlow.Application;

namespace HA.ModFlow.Application
{

    /// <summary>
    /// 应用程序级服务，提供应用程序，启动，配置模块，关闭等相关级别的操作
    /// </summary>
    public  class ApplicationService
    {

        protected ApplicationService()
        {
 
        }

        static ApplicationService _defalutService;

        public static ApplicationService DefalutService
        {
            get {

                if (_defalutService ==null)
                {
                    _defalutService = new ApplicationService(); 
                    
                }
                
                return ApplicationService._defalutService; 
            
            }

        }


        #region 功能区域

        /// <summary>
        /// 初始化所有组件系统，整个应用程序只需要调用一次
        /// </summary>
        public void InitModules()
        {
            //ProjectSerivce .DefaultService .ProjChanged

            //读取程序配置信息
            ConfigService.DefaultService.LoadConfig();

            ProjectService .DefaultService .ProjChanged+=new HAUtil.ProjHandler(DefaultService_ProjChanged);

            //ProjectSerivce .DefaultService .UINeedUpdate+=new HAUtil.UIHandler(DefaultService_UINeedUpdate);

            ProjectService.DefaultService.UIUpdatting += new EventHandler<ProjectUIEventArgs>(DefaultService_UIUpdatting);

            

 
        }

        void DefaultService_UIUpdatting(object sender, ProjectUIEventArgs e)
        {
            try
            {
                Render3dModule.DefaultModule.DefaultService_UIUpdatting(sender, e);

            }
            catch (Exception ex)
            {
                MessageBox.Show("三维图形更新错误："+ex.Message);
                
                
            }


            try
            {

                GisModule.DefaultModule.DefaultService_UIUpdatting(sender, e);

            }
            catch (Exception ex)
            {
                MessageBox.Show("二维图形更新错误：" + ex.Message);
            }

           


        }




        //void DefaultService_UINeedUpdate(HAUtil.UIRefreshArgs args)
        //{
           
        //    Render3dModule.DefaultModule.DefaultService_UINeedUpdate(args);

        //    GisModule.DefaultModule.DefaultService_UINeedUpdate(args);

          



            



        //}



        /// <summary>
        /// 更新所有需要跟随项目数据变化的视图
        /// </summary>
        /// <param name="chType"></param>
        void DefaultService_ProjChanged(HAUtil.ProjChangedType chType)
        {
           
            Render3dModule.DefaultModule.DefaultService_ProjChanged(chType);

            GisModule.DefaultModule.DefaultService_ProjChanged(chType);

           


            UIModule.DefaultModule.DefaultService_ProjChanged(chType);
        }


        /// <summary>
        /// 停止所有运行的模组，在程序关闭时调用
        /// </summary>
        public void DisposeModules()
        {
            ConfigService.DefaultService.SaveConfig ();

            Render3dModule.DefaultModule.DefaultService_ProjChanged(HAUtil.ProjChangedType.CLOSE );
 
        }

        /// <summary>
        /// 初始化UI配置信息
        /// </summary>
        public void InitUI()
        {
 
        }


        #endregion


        #region 公共服务部分

        /// <summary>
        /// 取得应用程序下的特定目录
        /// </summary>
        /// <param name="pathType"></param>
        /// <returns></returns>
        public string GetApplicationPath(ApplicationPath pathType)
        {
            string path = "";

            switch (pathType)
            {
                case ApplicationPath.MODELEXEPATH:

                      path = AppDomain.CurrentDomain.BaseDirectory+"bin\\";

              

                    break;
                case ApplicationPath.APPPATH:

                    path = AppDomain.CurrentDomain.BaseDirectory;

                    break;
                default:
                    break;
            }

            return path;



           
        }




        ///下面是项目操作相关的接口


        /// <summary>
        /// 打开一个项目
        /// </summary>
        public void OpenProject()
        {
            System.Windows.MessageBoxResult mbr = this.IsSaveBeforeClose();
            if (mbr == MessageBoxResult.Cancel)
            {
                return;
            }


            OpenFileDialog openfd = new OpenFileDialog();
            openfd.Filter = ".hmf文件（*.hmf）|*.hmf";
            if (openfd.ShowDialog() == true)
            {



                ProjectService.DefaultService.Open(@openfd.FileName);



                //程序配置操作

                if (!ProjectService.DefaultService.BNullProj)
                {
                   


                    History h = new History(DateTime.Now,ProjectService.DefaultService.GetProjMainFile(),ProjectService.DefaultService.GetProjName());

                    ConfigService.DefaultService.SettingInfo.AddHistory(h);
                    

                    
                }

               

            }
 
        }


        public void OpenProject(string filePath)
        {

            System.Windows.MessageBoxResult mbr = this.IsSaveBeforeClose();
            if (mbr == MessageBoxResult.Cancel)
            {
                return;
            }

            ProjectService.DefaultService.Open(filePath);



            //程序配置操作

            if (!ProjectService.DefaultService.BNullProj)
            {



                History h = new History(DateTime.Now, ProjectService.DefaultService.GetProjMainFile(), ProjectService.DefaultService.GetProjName());

                ConfigService.DefaultService.SettingInfo.AddHistory(h);



            }

 
        }

        /// <summary>
        /// 保存一个项目
        /// </summary>
        public void SaveProject()

        {
            if (!ProjectService .DefaultService .BNullProj)
            {
                ProjectService.DefaultService.Save();
                
            }

 
        }

        /// <summary>
        /// 关闭一个项目
        /// </summary>
        public void CloseProject()
        {
            this.IsSaveBeforeClose();
 
        }


        /// <summary>
        /// 关闭前是否保存
        /// </summary>
        protected MessageBoxResult IsSaveBeforeClose()
        {

            //首先判断当前项目是否为空

            if (!ProjectService.DefaultService.BNullProj)
            {
                System.Windows.MessageBoxResult mbr = System.Windows.MessageBox.Show("是否当前保存？", "提示", System.Windows.MessageBoxButton.YesNoCancel);

                //Console.WriteLine("返回值是：" + mbr.ToString());

                if (mbr == MessageBoxResult.Yes)
                {
                    ProjectService.DefaultService.Save();

                    ProjectService.DefaultService.CloseProj();
                }
                else if (mbr == MessageBoxResult.No)
                {
                    ProjectService.DefaultService.CloseProj();
                }
                else if (mbr == MessageBoxResult.Cancel)
                    return mbr;


                return mbr;

            }

            return MessageBoxResult.OK;

 
        }

        /// <summary>
        /// 应用程序创建新项目
        /// </summary>
        public void NewProject()
        {
            System.Windows.MessageBoxResult mbr = this.IsSaveBeforeClose();
            if (mbr==MessageBoxResult .Cancel )
            {
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = ".hmf文件（*.hmf）|*.hmf";
            if (sfd.ShowDialog() == true)
            {
                ProjectService .DefaultService .CreateNew(@sfd.FileName);

                ProjectService.DefaultService.Save();





                //程序配置操作

                if (!ProjectService.DefaultService.BNullProj)
                {



                    History h = new History(DateTime.Now, ProjectService.DefaultService.GetProjMainFile(), ProjectService.DefaultService.GetProjName());

                    ConfigService.DefaultService.SettingInfo.AddHistory(h);



                }





            }
 
        }

        #endregion

    }
}
